package com.xkstream.client.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.widget.ProgressBar;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by xiaoyi on 2017-11-21.
 */

public class LogFileUtil {
    LinkedList<String> queue = new LinkedList<>();
    RandomAccessFile accessFile;
    WorkThread workThread;
    File file;
    private static final String fileName = "legend_log.txt";


    private LogFileUtil() {
        init();
        workThread = new WorkThread();
        workThread.start();
    }

    private void init() {
        String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android/data/"
                + "com.xkstream.client" + File.separator + "files" + File.separator;
        file = new File(dir, fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            accessFile = new RandomAccessFile(file, "rw");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static volatile LogFileUtil mInstance;


    public static LogFileUtil getDefault() {
        if (mInstance == null) {
            synchronized (LogFileUtil.class) {
                if (mInstance == null) {
                    mInstance = new LogFileUtil();
                }
            }
        }
        return mInstance;
    }

    class WorkThread extends Thread {
        @Override
        public void run() {
            while (!isInterrupted()) {
                String log = get();
                if (TextUtils.isEmpty(log)) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        accessFile.writeUTF(log);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }

    private void checkFile() {
        if (!file.exists()) {
            init();
        }
    }

    public void put(String log) {
        checkFile();
        queue.addLast(log);
    }

    public void put(String tag, String log) {
        checkFile();
        queue.addLast(tag + " : " + log);
    }


    private String get() {
        if (queue.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder("Android   " + DateUtil.formatTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        sb.append(" | ").append(queue.poll()).append("\r\n");
        return sb.toString();
    }

    public void release() {
        workThread.interrupt();
        try {
            accessFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
